package com.hupu.jrs.ui.gallery

import android.annotation.SuppressLint
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import butterknife.BindView
import butterknife.ButterKnife
import com.facebook.drawee.view.SimpleDraweeView
import com.hupu.jrs.R
import com.hupu.jrs.bean.Folder
import java.io.File
import java.util.*
import javax.inject.Inject

/**
 * Created by sll on 2016/3/9.
 */
class FolderAdapter @Inject
constructor() : BaseAdapter() {

    private var folders: List<Folder> = ArrayList<Folder>()
    var selectIndex = 0
        set(i) {
            if (selectIndex == i) return
            field = i
            notifyDataSetChanged()
        }

    fun bind(folders: List<Folder>) {
        this.folders = folders
        notifyDataSetChanged()
    }

    private fun getTotalImageSize(mFolders: List<Folder>?): Int {
        var result = 0
        if (mFolders != null && mFolders.size > 0) {
            for (f in mFolders) {
                result += f.images!!.size
            }
        }
        return result
    }

    override fun getCount(): Int {
        return folders.size
    }

    override fun getItem(position: Int): Folder {
        return folders[position]
    }

    override fun getItemId(position: Int): Long {
        return 0
    }

    @SuppressLint("SetTextI18n")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        val holder: ViewHolder
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_list_folder, parent, false)
            holder = ViewHolder(convertView)
            convertView!!.tag = holder
        } else {
            holder = convertView.tag as ViewHolder
        }

        val folder = folders[position]
        if (position == 0) {
            holder.tvName!!.text = "所有图片"
            holder.tvSize!!.text = getTotalImageSize(folders).toString() + "张"
            if (!folders.isEmpty()) {
                holder.ivCover!!.setImageURI(Uri.fromFile(File(folder.cover!!.path)))
            }
        } else {
            holder.tvName!!.setText(folder.name)
            holder.tvSize!!.setText(folder.images!!.size.toString() + "张")
            holder.ivCover!!.setImageURI(Uri.fromFile(File(folder.cover!!.path)))
        }
        holder.ivIndicator!!.visibility = if (selectIndex == position) View.VISIBLE else View.GONE
        return convertView
    }

    internal class ViewHolder(view: View) {
        @BindView(R.id.ivCover)
        var ivCover: SimpleDraweeView? = null
        @BindView(R.id.tvName)
        var tvName: TextView? = null
        @BindView(R.id.tvSize)
        var tvSize: TextView? = null
        @BindView(R.id.ivIndicator)
        var ivIndicator: ImageView? = null

        init {
            ButterKnife.bind(this, view)
        }
    }
}
